package com.das.mechanic_base.adapter.groundpush;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.ground.GetDateTimeBean;
import com.das.mechanic_base.utils.X3ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class X3TimeAdapter extends RecyclerView.Adapter<X3TimeAdapter.GroupOrTimeHolder> {

    private Context mContext;

    private List<Boolean> groupList;//控制点击的状态

    private List<GetDateTimeBean> searchNumBean;

    public X3TimeAdapter(Context mContext) {
        this.mContext = mContext;
        groupList = new ArrayList<>();
        searchNumBean=new ArrayList<>();
    }

    public void setChangeGroup( List<GetDateTimeBean> searchNumBeans) {
        this.searchNumBean = searchNumBeans;
        this.groupList.clear();
        for (int i = 0; i < searchNumBean.size(); i++) {
            groupList.add(false);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GroupOrTimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new GroupOrTimeHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_time_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull GroupOrTimeHolder holder, int position) {
        String strDate = searchNumBean.get(position).getTimeLowRange() + " — " + searchNumBean.get(position).getTimeHignRange();
        holder.tv_title.setText(strDate);
        if (searchNumBean.get(position).isCanAppoint()) {

            if (groupList.get(position)) {
                // 点击选中
                holder.tv_title.setTextColor(Color.parseColor("#0077FF"));
                holder.tv_title.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_group_or_time_bg));
            } else {
                // 点击取消选中
                holder.tv_title.setTextColor(Color.parseColor("#333333"));
                holder.tv_title.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_shape_idea_unselect_item));
            }

            holder.itemView.setOnClickListener(view -> {
                for (int i = 0; i < groupList.size(); i++) {
                    if (i == position) {
                        if (!groupList.get(position)) {
                            groupList.remove(position);
                            groupList.add(position, true);
                        }
                    } else {
                        if (groupList.get(i)) {
                            groupList.remove(i);
                            groupList.add(i, false);
                        }
                    }
                }
                notifyDataSetChanged();

                if (iOnClickGroup != null) {
                    iOnClickGroup.iOnClickSelectGroup(strDate + "");
                }
            });
        } else {
            holder.tv_title.setTextColor(Color.parseColor("#999999"));
            holder.tv_title.setBackground(ContextCompat.getDrawable(mContext, R.drawable.x3_search_bg));
            holder.itemView.setOnClickListener(view -> {
                X3ToastUtils.showMessage(mContext.getString(R.string.x3_time_no_appiont));
            });
        }
    }

    IOnClickGroup iOnClickGroup;

    public void setiOnClickGroup(IOnClickGroup iOnClickGroup) {
        this.iOnClickGroup = iOnClickGroup;
    }

    public interface IOnClickGroup {
        void iOnClickSelectGroup(String ageRange);
    }

    @Override
    public int getItemCount() {
        return searchNumBean.size();
    }

    class GroupOrTimeHolder extends RecyclerView.ViewHolder {
        TextView tv_title;

        public GroupOrTimeHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
        }
    }
}
